package com.ac.scum.log.task.face.log.counsumer.stages.gamplay;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.scum.log.bean.ServerFTPLogBunkerActive;
import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.face.log.counsumer.stages.GameplayLogConsumer;
import com.ac.scum.log.task.mapper.log.ServerFTPLogBunkerActiveMapper;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import io.micrometer.common.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
@AllArgsConstructor
public class BunkerActiveConsumer {
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final ServerFTPLogBunkerActiveMapper serverFTPLogBunkerActiveMapper;

    public void bunkerActiveConsumer(List<LogConsumer.GameLogLine> lines, FTPQueueItem ftpQueueItem, GameplayLogConsumer gameplayLogConsumer) {
        try {
            Pattern activePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d+\\.\\d+): \\[LogBunkerLock\\] (\\w+) Bunker is Active\\. Activated (\\d+h \\d+m \\d+s) ago\\. X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            Pattern active2Pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker Activated (\\d+h \\d+m \\d+s) ago");
            Pattern active3Pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d+\\.\\d+): \\[LogBunkerLock\\] (\\w+) Bunker is Active\\. Activated (\\d+h \\d+m \\d+s) ago via Keycard\\. X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            Pattern deactivatedPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker Deactivated");
            Pattern nextActivePattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker is Locked. Locked (\\d+h \\d+m \\d+s) ago, next Activation in (\\d+h \\d+m \\d+s)\\. X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            Pattern nextActive2Pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker is Locked. Locked initially, next Activation in (\\d+h \\d+m \\d+s)\\. X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            Pattern activeByKeyCardPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Activated via Keycard (\\d+h \\d+m \\d+s) ago");
            Pattern activeByKeyCardPattern2 = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker Activated via Keycard (\\d+h \\d+m \\d+s) ago");
            Pattern waitingForNextActivationPattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): \\[LogBunkerLock\\] (\\w+) Bunker is Locked\\. Locked (\\d+h \\d+m \\d+s) ago, waiting for next activation info\\. X=(-?\\d+\\.\\d+) Y=(-?\\d+\\.\\d+) Z=(-?\\d+\\.\\d+)");
            List<ServerFTPLogBunkerActive> bunkerActives = Lists.newArrayList();
            for (LogConsumer.GameLogLine line : lines) {
                String lineContext = line.getLineContext();
                if (lineContext.contains("Bunker activations:") || lineContext.contains("Locked bunkers:")) {
                    continue;
                }
                Matcher active2Matcher = active2Pattern.matcher(lineContext);
                Matcher activeMatcher = activePattern.matcher(lineContext);
                Matcher active3Matcher = active3Pattern.matcher(lineContext);
                Matcher deactivatedMatcher = deactivatedPattern.matcher(lineContext);
                Matcher nextActiveMatcher = nextActivePattern.matcher(lineContext);
                Matcher nextActive2Matcher = nextActive2Pattern.matcher(lineContext);
                Matcher activeByKeyCardMatcher = activeByKeyCardPattern.matcher(lineContext);
                Matcher activeByKeyCardMatcher2 = activeByKeyCardPattern2.matcher(lineContext);
                Matcher waitingForNextActivationMatcher = waitingForNextActivationPattern.matcher(lineContext);

                int action = 0;
                String serverTime = "";
                String bunkerName = "";
                String activationTime = "";
                String nextActivationTime = "";
                if (activeMatcher.find()) {
                    action = 1;
                    serverTime = activeMatcher.group(1);
                    bunkerName = activeMatcher.group(2);
                    activationTime = activeMatcher.group(3);
                } else if (active2Matcher.find()) {
                    action = 1;
                    serverTime = active2Matcher.group(1);
                    bunkerName = active2Matcher.group(2);
                    activationTime = active2Matcher.group(3);
                } else if (deactivatedMatcher.find()) {
                    action = 2;
                    serverTime = deactivatedMatcher.group(1);
                    bunkerName = deactivatedMatcher.group(2);
                } else if (nextActiveMatcher.find()) {
                    action = 2;
                    serverTime = nextActiveMatcher.group(1);
                    bunkerName = nextActiveMatcher.group(2);
                    activationTime = nextActiveMatcher.group(3);
                    nextActivationTime = nextActiveMatcher.group(4);
                } else if (nextActive2Matcher.find()) {
                    action = 2;
                    serverTime = nextActive2Matcher.group(1);
                    bunkerName = nextActive2Matcher.group(2);
                    nextActivationTime = nextActive2Matcher.group(3);
                } else if (activeByKeyCardMatcher.find()) {
                    action = 1;
                    serverTime = activeByKeyCardMatcher.group(1);
                    bunkerName = activeByKeyCardMatcher.group(2);
                    activationTime = activeByKeyCardMatcher.group(3);
                } else if (activeByKeyCardMatcher2.find()) {
                    action = 1;
                    serverTime = activeByKeyCardMatcher2.group(1);
                    bunkerName = activeByKeyCardMatcher2.group(2);
                    activationTime = activeByKeyCardMatcher2.group(3);
                } else if (active3Matcher.find()) {
                    action = 1;
                    serverTime = active3Matcher.group(1);
                    bunkerName = active3Matcher.group(2);
                    activationTime = active3Matcher.group(3);
                } else if (waitingForNextActivationMatcher.find()) {
                    action = 2;
                    serverTime = waitingForNextActivationMatcher.group(1);
                    bunkerName = waitingForNextActivationMatcher.group(2);
                    activationTime = waitingForNextActivationMatcher.group(3);
                }  else {
                    gameplayLogConsumer.commitDiscardLog(ftpQueueItem, line, "BunkerActive no match");
                    continue;
                }
                ServerFTPLogBunkerActive logBunkerActive = new ServerFTPLogBunkerActive();
                logBunkerActive.setId(snowflakeIdGenerator.nextId());
                logBunkerActive.setFileId(ftpQueueItem.getFileId());
                logBunkerActive.setServerId(ftpQueueItem.getServer().getId());
                logBunkerActive.setLineStart(line.getStartByte());
                logBunkerActive.setLineEnd(line.getEndByte());
                logBunkerActive.setServerTime(gameplayLogConsumer.parseTimestamp(serverTime, ftpQueueItem));
                logBunkerActive.setType(action);
                logBunkerActive.setBunkerName(bunkerName);
                logBunkerActive.setCreateTime(LocalDateTime.now());
                bunkerActives.add(logBunkerActive);
                if (StringUtils.isNotBlank(activationTime)) {
                    logBunkerActive.setActiveTime(logBunkerActive.getServerTime().minusSeconds(getBunkerActiveTime(activationTime)));
                }
                if (StringUtils.isNotBlank(nextActivationTime)) {
                    logBunkerActive.setNextActiveTime(logBunkerActive.getServerTime().plusSeconds(getBunkerActiveTime(nextActivationTime)));
                }
            }
            bunkerActives.forEach(serverFTPLogBunkerActiveMapper::insert);
        } catch (Exception e) {
            log.error("地堡激活日志任务异常", e);
        }

    }

    public Long getBunkerActiveTime(String time) {
        if (time.isBlank()) {
            return 0L;
        }
        time = time.replaceAll(" ", "");
        String[] split = time.split("h");
        long h = Long.parseLong(split[0]);
        split = split[1].split("m");
        long m = Long.parseLong(split[0]);
        split = split[1].split("s");
        long s = Long.parseLong(split[0]);
        return ((h * 60 * 60) + (m * 60) + s);
    }
}
